package com.gprinter.sample;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.io.FileOutputStream;
import java.util.Hashtable;

public class QRCodeUtil
{
    public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath)
    {
        try
        {
            if ((content == null) || ("".equals(content))) {
                return false;
            }

            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

            hints.put(EncodeHintType.MARGIN, Integer.valueOf(1));

            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
            int[] pixels = new int[widthPix * heightPix];

            for (int y = 0; y < heightPix; y++) {
                for (int x = 0; x < widthPix; x++) {
                    if (bitMatrix.get(x, y))
                        pixels[(y * widthPix + x)] = -16777216;
                    else {
                        pixels[(y * widthPix + x)] = -1;
                    }
                }

            }

            Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);

            if (logoBm != null) {
                bitmap = addLogo(bitmap, logoBm);
            }

            return (bitmap != null) && (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Bitmap createQRImage(String content, int widthPix, int heightPix)
    {
        try
        {
            if ((content == null) || ("".equals(content))) {
                return null;
            }

            Hashtable hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

            hints.put(EncodeHintType.MARGIN, Integer.valueOf(1));

            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);
            int[] pixels = new int[widthPix * heightPix];

            for (int y = 0; y < heightPix; y++) {
                for (int x = 0; x < widthPix; x++) {
                    if (bitMatrix.get(x, y))
                        pixels[(y * widthPix + x)] = -16777216;
                    else {
                        pixels[(y * widthPix + x)] = -1;
                    }
                }

            }

            Bitmap bitmap = Bitmap.createBitmap(widthPix, heightPix, Config.RGB_565);
            bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);

            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Bitmap addLogo(Bitmap src, Bitmap logo)
    {
        if (src == null) {
            return null;
        }
        if (logo == null) {
            return src;
        }

        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();

        if ((srcWidth == 0) || (srcHeight == 0)) {
            return null;
        }

        if ((logoWidth == 0) || (logoHeight == 0)) {
            return src;
        }

        float scaleFactor = srcWidth * 1.0F / 5.0F / logoWidth;
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(src, 0.0F, 0.0F, null);
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
            canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);

            canvas.save();
            canvas.restore();
        } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
        }

        return bitmap;
    }
}